package buffered;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * BufferedReader类:
 *  构造方法:
 *      public BufferedReader(Reader in):将基本字符输入流包装成缓冲(缓存)流
 *  成员方法:
 *      public int read() throws IOException:对于字符缓冲流来说,就是从底层的字符数组中读取一个字符并返回
 *      public String readLine() throws IOException:当读到换行标志或者文件内容的结束标志,就停止,把之前独到的字符作为一个字符串返回
 *      public void close() throws IOException:关流释放资源
 *  BufferedReader底层原理:
 *      1.底层使用的是一个长度为8192的字符数组(char[] cb = new char[8192])
 *      2.每次调用read()方法会尽可能多的读取文件中的字符到底层字符数组中
 *          每次最多不超过8192
 */
public class BufferedReaderDemo01 {
    public static void main(String[] args)throws IOException {
        //method01();
        BufferedReader br = new BufferedReader(new FileReader("day23-IO-02/10.txt"));
/*        System.out.println(br.readLine());//神雕侠侣
        System.out.println(br.readLine());//天龙八部
        System.out.println(br.readLine());//射雕英雄传
        System.out.println(br.readLine());//null 如果读到文件末尾返回null
        */

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }

    private static void method01() throws IOException {
        //先用FileReader关联9.txt,然后再利用BufferedReader对FileReader进行缓冲
        BufferedReader br = new BufferedReader(new FileReader("day23-IO-02/9.txt"));
        int ch;
        while ((ch = br.read()) != -1) {
            System.out.println((char) ch);
        }
        br.close();
    }
}
